package groep3.ssc.tmts;


import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;


public class TMTSSimulator implements Runnable
{
    private long time = 0;
    private int lastTime = 0;
    private int distance = 0;
    private boolean running = false;
    private TMTSListener tmtsListener;
    private final int TIME_MULTIPLICATION = 4;

    public TMTSSimulator(TMTSListener tmtsl)
    {
        tmtsListener = tmtsl;
        running = true;
    }

    public void stop()
    {
        running = false;
    }

    public long getTime()
    {
        return time;
    }

    @Override
    public void run()
    {
        Random r = new Random();
        while (running)
        {
            try
            {
                lastTime = 28000 + r.nextInt(5000);
                time += lastTime;
                distance += 400;
                Thread.sleep(lastTime / TIME_MULTIPLICATION);

                JSONObject input = new JSONObject();
                input.put("time", time);
                input.put("lapTime", lastTime);
                input.put("totalDistance", distance);
                input.put("skaterID", 240);
                tmtsListener.givePulse(input.toString());
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
